home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / lib / perl5 / Net / DBus / Exporter.pm < prev    next >
Encoding:
Perl POD Document  |  2008-02-20  |  15.7 KB  |  578 lines

  1. # -*- perl -*-
  2. #
  3. # Copyright (C) 2004-2006 Daniel P. Berrange
  4. #
  5. # This program is free software; You can redistribute it and/or modify
  6. # it under the same terms as Perl itself. Either:
  7. #
  8. # a) the GNU General Public License as published by the Free
  9. #   Software Foundation; either version 2, or (at your option) any
  10. #   later version,
  11. #
  12. # or
  13. #
  14. # b) the "Artistic License"
  15. #
  16. # The file "COPYING" distributed along with this file provides full
  17. # details of the terms and conditions of the two licenses.
  18.  
  19. =pod
  20.  
  21. =head1 NAME
  22.  
  23. Net::DBus::Exporter - Export object methods and signals to the bus
  24.  
  25. =head1 SYNOPSIS
  26.  
  27.   # Define a new package for the object we're going
  28.   # to export
  29.   package Demo::HelloWorld;
  30.  
  31.   # Specify the main interface provided by our object
  32.   use Net::DBus::Exporter qw(org.example.demo.Greeter);
  33.  
  34.   # We're going to be a DBus object
  35.   use base qw(Net::DBus::Object);
  36.  
  37.   # Export a 'Greeting' signal taking a stringl string parameter
  38.   dbus_signal("Greeting", ["string"]);
  39.  
  40.   # Export 'Hello' as a method accepting a single string
  41.   # parameter, and returning a single string value
  42.   dbus_method("Hello", ["string"], ["string"]);
  43.  
  44.   # Export 'Goodbye' as a method accepting a single string
  45.   # parameter, and returning a single string, but put it
  46.   # in the 'org.exaple.demo.Farewell' interface
  47.   dbus_method("Goodbye", ["string"], ["string"], "org.example.demo.Farewell");
  48.  
  49. =head1 DESCRIPTION
  50.  
  51. The C<Net::DBus::Exporter> module is used to export methods
  52. and signals defined in an object to the message bus. Since
  53. Perl is a loosely typed language it is not possible to automatically
  54. determine correct type information for methods to be exported.
  55. Thus when sub-classing L<Net::DBus::Object>, this package will
  56. provide the type information for methods and signals.
  57.  
  58. When importing this package, an optional argument can be supplied
  59. to specify the default interface name to associate with methods
  60. and signals, for which an explicit interface is not specified.
  61. Thus in the common case of objects only providing a single interface,
  62. this removes the need to repeat the interface name against each
  63. method exported.
  64.  
  65. =head1 SCALAR TYPES
  66.  
  67. When specifying scalar data types for parameters and return values,
  68. the following string constants must be used to denote the data
  69. type. When values corresponding to these types are (un)marshalled
  70. they are represented as the Perl SCALAR data type (see L<perldata>).
  71.  
  72. =over 4
  73.  
  74. =item "string"
  75.  
  76. A UTF-8 string of characters
  77.  
  78. =item "int16"
  79.  
  80. A 16-bit signed integer
  81.  
  82. =item "uint16"
  83.  
  84. A 16-bit unsigned integer
  85.  
  86. =item "int32"
  87.  
  88. A 32-bit signed integer
  89.  
  90. =item "uint32"
  91.  
  92. A 32-bit unsigned integer
  93.  
  94. =item "int64"
  95.  
  96. A 64-bit signed integer. NB, this type is not supported by
  97. many builds of Perl on 32-bit platforms, so if used, your
  98. data is liable to be truncated at 32-bits.
  99.  
  100. =item "uint64"
  101.  
  102. A 64-bit unsigned integer. NB, this type is not supported by
  103. many builds of Perl on 32-bit platforms, so if used, your
  104. data is liable to be truncated at 32-bits.
  105.  
  106. =item "byte"
  107.  
  108. A single 8-bit byte
  109.  
  110. =item "bool"
  111.  
  112. A boolean value
  113.  
  114. =item "double"
  115.  
  116. An IEEE double-precision floating point
  117.  
  118. =back
  119.  
  120. =head1 COMPOUND TYPES
  121.  
  122. When specifying compound data types for parameters and return
  123. values, an array reference must be used, with the first element
  124. being the name of the compound type. 
  125.  
  126. =over 4
  127.  
  128. =item ["array", ARRAY-TYPE]
  129.  
  130. An array of values, whose type os C<ARRAY-TYPE>. The C<ARRAY-TYPE>
  131. can be either a scalar type name, or a nested compound type. When
  132. values corresponding to the array type are (un)marshalled, they 
  133. are represented as the Perl ARRAY data type (see L<perldata>). If,
  134. for example, a method was declared to have a single parameter with
  135. the type, ["array", "string"], then when calling the method one
  136. would provide a array reference of strings:
  137.  
  138.     $object->hello(["John", "Doe"])
  139.  
  140. =item ["dict", KEY-TYPE, VALUE-TYPE]
  141.  
  142. A dictionary of values, more commonly known as a hash table. The
  143. C<KEY-TYPE> is the name of the scalar data type used for the dictionary
  144. keys. The C<VALUE-TYPE> is the name of the scalar, or compound
  145. data type used for the dictionary values. When values corresponding
  146. to the dict type are (un)marshalled, they are represented as the
  147. Perl HASH data type (see L<perldata>). If, for example, a method was
  148. declared to have a single parameter with the type ["dict", "string", "string"],
  149. then when calling the method one would provide a hash reference 
  150. of strings,
  151.  
  152.    $object->hello({forename => "John", surname => "Doe"});
  153.  
  154. =item ["struct", VALUE-TYPE-1, VALUE-TYPE-2]
  155.  
  156. A structure of values, best thought of as a variation on the array
  157. type where the elements can vary. Many languages have an explicit
  158. name associated with each value, but since Perl does not have a
  159. native representation of structures, they are represented by the
  160. LIST data type. If, for exaple, a method was declared to have a single
  161. parameter with the type ["struct", "string", "string"], corresponding
  162. to the C structure 
  163.  
  164.     struct {
  165.       char *forename;
  166.       char *surname;
  167.     } name;
  168.  
  169. then, when calling the method one would provide an array refernce
  170. with the values orded to match the structure
  171.  
  172.    $object->hello(["John", "Doe"]);
  173.  
  174. =back
  175.  
  176. =head1 MAGIC TYPES
  177.  
  178. When specifying introspection data for an exported service, there
  179. are a couple of so called C<magic> types. Parameters declared as
  180. magic types are not visible to clients, but instead their values
  181. are provided automatically by the server side bindings. One use of
  182. magic types is to get an extra parameter passed with the unique 
  183. name of the caller invoking the method.
  184.  
  185. =over 4
  186.  
  187. =item "caller"
  188.  
  189. The value passed in is the unique name of the caller of the method.
  190. Unique names are strings automatically assigned to client connections
  191. by the bus daemon, for example ':1.15'
  192.  
  193. =item "serial"
  194.  
  195. The value passed in is an integer within the scope of a caller, which 
  196. increments on every method call. 
  197.  
  198. =back
  199.  
  200. =head1 ANNOTATIONS
  201.  
  202. When exporting methods, signals & properties, in addition to the core
  203. data typing information, a number of metadata annotations are possible.
  204. These are specified by passing a hash reference with the desired keys
  205. as the last parameter when defining the export. The following annotations
  206. are currently supported
  207.  
  208. =over 4
  209.  
  210. =item no_return
  211.  
  212. Indicate that this method does not return any value, and thus no reply
  213. message should be sent over the wire, likewise informing the clients
  214. not to expect / wait for a reply message
  215.  
  216. =item deprecated
  217.  
  218. Indicate that use of this method/signal/property is discouraged, and
  219. it may disappear altogether in a future release. Clients will typically
  220. print out a warning message when a deprecated method/signal/property
  221. is used.
  222.  
  223. =item param_names
  224.  
  225. An array of strings specifying names for the input parameters of the
  226. method or signal. If omitted, no names will be assigned.
  227.  
  228. =item return_names
  229.  
  230. An array of strings specifying names for the return parameters of the
  231. method. If omitted, no names will be assigned.
  232.  
  233. =back
  234.  
  235. =head1 METHODS
  236.  
  237. =over 4
  238.  
  239. =cut
  240.  
  241. package Net::DBus::Exporter;
  242.  
  243. use vars qw(@ISA @EXPORT %dbus_exports %dbus_introspectors);
  244.  
  245. use Net::DBus::Binding::Introspector;
  246.  
  247. use warnings;
  248. use strict;
  249.  
  250. use Exporter;
  251. @ISA = qw(Exporter);
  252.  
  253. @EXPORT = qw(dbus_method dbus_signal dbus_property);
  254.  
  255.  
  256. sub import {
  257.     my $class = shift;
  258.  
  259.     my $caller = caller;
  260.     if (exists $dbus_exports{$caller}) {
  261.     warn "$caller is already registered with Net::DBus::Exporter";
  262.     return;
  263.     }
  264.  
  265.     $dbus_exports{$caller} = {
  266.     methods => {},
  267.     signals => {},
  268.     props => {},
  269.     };
  270.     die "usage: use Net::DBus::Exporter 'interface-name';" unless @_;
  271.  
  272.     my $interface = shift;
  273.     die "interface name '$interface' is not valid." .
  274.     "Names must consist of tokens using the characters a-z, A-Z, 0-9, _, " .
  275.     "with at least two tokens, separated by '.'\n"
  276.     unless $interface =~ /^[a-zA-Z]\w*(\.[a-zA-Z]\w*)+$/;
  277.     $dbus_exports{$caller}->{interface} = $interface;
  278.  
  279.     $class->export_to_level(1, "", @EXPORT);
  280. }
  281.  
  282. sub _dbus_introspector {
  283.     my $class = shift;
  284.  
  285.     if (!exists $dbus_exports{$class}) {
  286.     # If this class has not been exported, lets look
  287.     # at the parent class & return its introspection
  288.         # data instead.
  289.     no strict 'refs';
  290.     if (defined (*{"${class}::ISA"})) {
  291.         my @isa = @{"${class}::ISA"};
  292.         foreach my $parent (@isa) {
  293.         # We don't recurse to Net::DBus::Object
  294.         # since we need to give sub-classes the
  295.         # choice of not supporting introspection
  296.         next if $parent eq "Net::DBus::Object";
  297.  
  298.         my $ins = &_dbus_introspector($parent);
  299.         if ($ins) {
  300.             return $ins;
  301.         }
  302.         }
  303.     }
  304.     return undef;
  305.     }
  306.  
  307.     unless (exists $dbus_introspectors{$class}) {
  308.     my $is = Net::DBus::Binding::Introspector->new();
  309.     &_dbus_introspector_add($class, $is);
  310.     $dbus_introspectors{$class} = $is;
  311.     }
  312.     
  313.     return $dbus_introspectors{$class};
  314. }
  315.  
  316. sub _dbus_introspector_add {
  317.     my $class = shift;
  318.     my $introspector = shift;
  319.  
  320.     my $exports = $dbus_exports{$class};
  321.     if ($exports) {
  322.     foreach my $method (keys %{$exports->{methods}}) {
  323.         my ($params, $returns, $interface, $attributes, $paramnames, $returnnames) = @{$exports->{methods}->{$method}};
  324.         $introspector->add_method($method, $params, $returns, $interface, $attributes, $paramnames, $returnnames);
  325.     }
  326.     foreach my $prop (keys %{$exports->{props}}) {
  327.         my ($type, $access, $interface, $attributes) = @{$exports->{props}->{$prop}};
  328.         $introspector->add_property($prop, $type, $access, $interface, $attributes);
  329.     }
  330.     foreach my $signal (keys %{$exports->{signals}}) {
  331.         my ($params, $interface, $attributes, $paramnames) = @{$exports->{signals}->{$signal}};
  332.         $introspector->add_signal($signal, $params, $interface, $attributes, $paramnames);
  333.     }
  334.     }
  335.     
  336.     if (defined (*{"${class}::ISA"})) {
  337.     no strict "refs";
  338.     my @isa = @{"${class}::ISA"};
  339.     foreach my $parent (@isa) {
  340.         &_dbus_introspector_add($parent, $introspector);
  341.     }
  342.     }
  343. }
  344.  
  345. =item dbus_method($name, $params, $returns, [\%annotations]);
  346.  
  347. =item dbus_method($name, $params, $returns, $interface, [\%annotations]);
  348.  
  349. Exports a method called C<$name>, having parameters whose types
  350. are defined by C<$params>, and returning values whose types are
  351. defined by C<$returns>. If the C<$interface> parameter is 
  352. provided, then the method is associated with that interface, otherwise
  353. the default interface for the calling package is used. The
  354. value for the C<$params> parameter should be an array reference
  355. with each element defining the data type of a parameter to the
  356. method. Likewise, the C<$returns> parameter should be an array 
  357. reference with each element defining the data type of a return
  358. value. If it not possible to export a method which accepts a
  359. variable number of parameters, or returns a variable number of
  360. values.
  361.  
  362. =cut
  363.  
  364. sub dbus_method {
  365.     my $name = shift;
  366.     my $params = [];
  367.     my $returns = [];
  368.     my $caller = caller;
  369.     my $interface = $dbus_exports{$caller}->{interface};
  370.     my %attributes;
  371.     
  372.     if (@_ && ref($_[0]) eq "ARRAY") {
  373.     $params = shift;
  374.     }
  375.     if (@_ && ref($_[0]) eq "ARRAY") {
  376.     $returns = shift;
  377.     }
  378.     if (@_ && !ref($_[0])) {
  379.     $interface = shift;
  380.     }
  381.     if (@_ && ref($_[0]) eq "HASH") {
  382.     %attributes = %{$_[0]};
  383.     }
  384.  
  385.     if (!$interface) {
  386.     die "interface not specified & no default interface defined";
  387.     }
  388.  
  389.     my $param_names = [];
  390.     if ( $attributes{param_names} ) {
  391.       $param_names = $attributes{param_names} if ref($attributes{param_names}) eq "ARRAY";
  392.       delete($attributes{param_names});
  393.     }
  394.     my $return_names = [];
  395.     if ( $attributes{return_names} ) {
  396.       $return_names = $attributes{return_names} if ref($attributes{return_names}) eq "ARRAY";
  397.       delete($attributes{return_names});
  398.     }
  399.  
  400.     $dbus_exports{$caller}->{methods}->{$name} = [$params, $returns, $interface, \%attributes, $param_names, $return_names];
  401. }
  402.  
  403.  
  404. =item dbus_property($name, $type, $access, [\%attributes]);
  405.  
  406. =item dbus_property($name, $type, $access, $interface, [\%attributes]);
  407.  
  408. Exports a property called C<$name>, whose data type is C<$type>.
  409. If the C<$interface> parameter is provided, then the property is 
  410. associated with that interface, otherwise the default interface 
  411. for the calling package is used. 
  412.  
  413. =cut
  414.  
  415. sub dbus_property {
  416.     my $name = shift;
  417.     my $type = "string";
  418.     my $access = "readwrite";
  419.     my $caller = caller;
  420.     my $interface = $dbus_exports{$caller}->{interface};
  421.     my %attributes;
  422.     
  423.     if (@_ && (!ref($_[0]) || (ref($_[0]) eq "ARRAY"))) {
  424.     $type = shift;
  425.     }
  426.     if (@_ && !ref($_[0])) {
  427.     $access = shift;
  428.     }
  429.     if (@_ && !ref($_[0])) {
  430.     $interface = shift;
  431.     }
  432.     if ($_ && ref($_[0]) eq "HASH") {
  433.     %attributes = %{$_[0]};
  434.     }
  435.  
  436.     if (!$interface) {
  437.     die "interface not specified & no default interface defined";
  438.     }
  439.  
  440.     $dbus_exports{$caller}->{props}->{$name} = [$type, $access, $interface, \%attributes];
  441. }
  442.  
  443.  
  444. =item dbus_signal($name, $params, [\%attributes]);
  445.  
  446. =item dbus_signal($name, $params, $interface, [\%attributes]);
  447.  
  448. Exports a signal called C<$name>, having parameters whose types
  449. are defined by C<$params>, and returning values whose types are
  450. defined by C<$returns>. If the C<$interface> parameter is 
  451. provided, then the signal is associated with that interface, otherwise
  452. the default interface for the calling package is used. The
  453. value for the C<$params> parameter should be an array reference
  454. with each element defining the data type of a parameter to the
  455. signal. Signals do not have return values. It not possible to 
  456. export a signal which has a variable number of parameters.
  457.  
  458. =cut
  459.  
  460. sub dbus_signal {
  461.     my $name = shift;
  462.     my $params = [];
  463.     my $caller = caller;
  464.     my $interface = $dbus_exports{$caller}->{interface};
  465.     my %attributes;
  466.     
  467.     if (@_ && ref($_[0]) eq "ARRAY") {
  468.     $params = shift;
  469.     }
  470.     if (@_ && !ref($_[0])) {
  471.     $interface = shift;
  472.     }
  473.     if (@_ && ref($_[0]) eq "HASH") {
  474.     %attributes = %{$_[0]};
  475.     }
  476.  
  477.     if (!$interface) {
  478.     die "interface not specified & no default interface defined";
  479.     }
  480.  
  481.     my $param_names = [];
  482.     if ( $attributes{param_names} ) {
  483.       $param_names = $attributes{param_names} if ref($attributes{param_names}) eq "ARRAY";
  484.       delete($attributes{param_names});
  485.     }
  486.  
  487.     $dbus_exports{$caller}->{signals}->{$name} = [$params, $interface, \%attributes, $param_names];
  488. }
  489.  
  490. 1;
  491.  
  492. =back
  493.  
  494. =head1 EXAMPLES
  495.  
  496. =over 4
  497.  
  498. =item No paramters, no return values
  499.  
  500. A method which simply prints "Hello World" each time its called
  501.  
  502.    sub Hello {
  503.        my $self = shift;
  504.        print "Hello World\n";
  505.    }
  506.  
  507.    dbus_method("Hello", [], []);
  508.  
  509. =item One string parameter, returning an boolean value
  510.  
  511. A method which accepts a process name, issues the killall
  512. command on it, and returns a boolean value to indicate whether
  513. it was successful.
  514.  
  515.    sub KillAll {
  516.        my $self = shift;
  517.        my $processname = shift;
  518.        my $ret  = system("killall $processname");
  519.        return $ret == 0 ? 1 : 0;
  520.    }
  521.  
  522.    dbus_method("KillAll", ["string"], ["bool"]);
  523.  
  524. =item One list of strings parameter, returning a dictionary
  525.  
  526. A method which accepts a list of files names, stats them, and
  527. returns a dictionary containing the last modification times.
  528.  
  529.     sub LastModified {
  530.        my $self = shift;
  531.        my $files = shift;
  532.  
  533.        my %mods;
  534.        foreach my $file (@{$files}) {
  535.           $mods{$file} = (stat $file)[9];
  536.        }
  537.        return \%mods;
  538.     }
  539.  
  540.     dbus_method("LastModified", ["array", "string"], ["dict", "string", "int32"]);
  541.  
  542. =item Annotating methods with metdata
  543.  
  544. A method which is targetted for removal, and also does not
  545. return any value
  546.  
  547.     sub PlayMP3 {
  548.     my $self = shift;
  549.         my $track = shift;
  550.  
  551.         system "mpg123 $track &";
  552.     }
  553.  
  554.     dbus_method("PlayMP3", ["string"], [], { deprecated => 1, no_return => 1 });
  555.  
  556. Or giving names to input parameters:
  557.  
  558.     sub PlayMP3 {
  559.     my $self = shift;
  560.         my $track = shift;
  561.  
  562.         system "mpg123 $track &";
  563.     }
  564.  
  565.     dbus_method("PlayMP3", ["string"], [], { param_names => ["track"] });
  566.  
  567. =back
  568.  
  569. =head1 SEE ALSO
  570.  
  571. L<Net::DBus::Object>, L<Net::DBus::Binding::Introspector>
  572.  
  573. =head1 AUTHORS
  574.  
  575. Daniel P. Berrange <dan@berrange.com>
  576.  
  577. =cut
  578.